package com.itheima.springrabbitmqproducer.controller;

import com.itheima.springrabbitmqproducer.config.RabbitMQConfig;
import com.itheima.springrabbitmqproducer.dao.StudentMapper;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author wxw
 * @data 2021/1/1 20 :35
 * @description
 */
@Controller
public class StudentController {
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @ResponseBody
    @RequestMapping(value = "/add" ,method = {RequestMethod.POST})
    public String addStudent(@RequestParam("sname") String sname){
        rabbitTemplate.convertAndSend(RabbitMQConfig.ITEM_TOPIC_EXCHANGE,"item.insert",sname);
        studentMapper.addStudent(sname);
         return "添加成功";
    }
    @RabbitListener(queues = "item_queue")
    public  void myListener1(String message){
        String newname=message+"cnm";
        System.out.println("消费者接收到的消息为："+newname);
        studentMapper.addStudent(newname);
    }
}
